package com.otoe.dilidilivid.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.Date;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.otoe.dilidilivid.data.entity.CommentInfo;
import com.otoe.dilidilivid.R;

public class CommentListAdapter extends RecyclerView.Adapter<CommentListAdapter.VH> {

    private List<CommentInfo> mDatas;
    public CommentListAdapter(List<CommentInfo> datas){
        this.mDatas = datas;
    }
    public static class VH extends RecyclerView.ViewHolder{
        public TextView name;
        public TextView time;
        public TextView content;
        public TextView likeNum;
        public Context context;
        public VH(View v){
            super(v);
            name=(TextView) v.findViewById(R.id.tv_name);
            time=(TextView) v.findViewById(R.id.tv_time);
            content=(TextView) v.findViewById(R.id.tv_content);
            likeNum=(TextView) v.findViewById(R.id.tv_like_num);
            context = v.getContext();
        }
    }

    @Override
    public void onBindViewHolder(@NonNull CommentListAdapter.VH holder, int position) {
        holder.name.setText(mDatas.get(position).getUserName());
        holder.time.setText(mDatas.get(position).getTime());
        holder.content.setText(mDatas.get(position).getContent());
        holder.likeNum.setText(mDatas.get(position).getLikeNum()+"");
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //item点击事件

            }
        });
    }

    @Override
    public int getItemCount() {
        return mDatas.size();
    }


    @Override
    public CommentListAdapter.VH onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = View.inflate(parent.getContext(), R.layout.comment_list_item, null);
        return new CommentListAdapter.VH(v);
    }

    //添加评论项
    public void addItem(String text){
        CommentInfo commentInfo=new CommentInfo();
        commentInfo.setContent(text);
        commentInfo.setUserName("小凯");
        Date date=new Date();
        commentInfo.setTime(date.getHours()+":"+date.getMinutes());
        mDatas.add(0,commentInfo);
        notifyItemInserted(0);
    }
}
